package com.recruit.user;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.HttpResponse;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jeecg.recruitcompanyinfo.entity.RecruitCompanyInfoEntity;
import com.jeecg.recruitcompanyjob.entity.RecruitCompanyJobEntity;
import com.jeecg.recruituser.entity.RecruitUserEntity;
import com.recruit.index.IndexVo;
import com.recruit.index.TypeUtil;



@Controller
@RequestMapping("/userLoginContoller")
public class UserLoginController {
	
	@Autowired
	private SystemService systemService;
	
	@RequestMapping(params = "toLogin")
	public ModelAndView toLogin(HttpServletRequest request,ModelMap modelMap) {
		modelMap.addAttribute("msg", 3);
		return new ModelAndView("com/recruit/user/login");
	}
	
	
	@RequestMapping(params = "toRegister")
	public ModelAndView toRegister(HttpServletRequest request,ModelMap modelMap) {
		return new ModelAndView("com/recruit/user/register");
	}
	

	@RequestMapping(params = "doLogin")
	public ModelAndView doLogin(HttpSession session,HttpServletRequest request,ModelMap modelMap,String userName,String password) {
		RecruitUserEntity checkuser = systemService.findUniqueByProperty(RecruitUserEntity.class, "username", userName);
		if(StringUtil.isEmpty(checkuser)){
			modelMap.addAttribute("msg", 0);
			return new ModelAndView("com/recruit/user/login");
		}else{
			if(checkuser.getPassword().equals(password)){
				session.setAttribute("username", userName);
				//随机获取4条热门岗位
				String hql = "from RecruitCompanyJobEntity order by RAND()";
				List<RecruitCompanyJobEntity> recruitCompanyJobEntity = systemService.findByQueryString(hql);
				List<IndexVo> hotJob = new ArrayList<>();
				IndexVo indexVo;
				if(recruitCompanyJobEntity.size()>4){
					for (int i=0;i<4;i++ ) {
						RecruitCompanyJobEntity job = new RecruitCompanyJobEntity();
						BeanUtils.copyProperties(recruitCompanyJobEntity.get(i), job);
						job.setEducation(TypeUtil.getEduName(job.getEducation()));
						
						RecruitCompanyInfoEntity company = systemService.findUniqueByProperty(RecruitCompanyInfoEntity.class, "userId", job.getCompanyId());
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(company, companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(company.getDevelopState()));
						indexVo = new IndexVo();
						indexVo.setJob(job);
						indexVo.setCompany(companyVo);
						hotJob.add(indexVo);
					}
				}else{
					for (RecruitCompanyJobEntity recruitCompanyJobEntity2 : recruitCompanyJobEntity) {
						RecruitCompanyJobEntity job = new RecruitCompanyJobEntity();
						BeanUtils.copyProperties(recruitCompanyJobEntity2, job);
						job.setEducation(TypeUtil.getEduName(job.getEducation()));
						RecruitCompanyInfoEntity company = systemService.findUniqueByProperty(RecruitCompanyInfoEntity.class, "userId", job.getCompanyId());
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(company, companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(company.getDevelopState()));
						indexVo = new IndexVo();
						indexVo.setJob(job);
						indexVo.setCompany(companyVo);
						hotJob.add(indexVo);
					}
				}
				
				//随机获取4个热门公司
				String hql2 = "from RecruitCompanyInfoEntity order by RAND()";
				List<RecruitCompanyInfoEntity> recruitCompanyInfoEntity = systemService.findByQueryString(hql2);
				List<RecruitCompanyInfoEntity> hotCompany = new ArrayList<>();
				if(recruitCompanyInfoEntity.size()>4){
					for (int i=0;i<4;i++ ) {
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(recruitCompanyInfoEntity.get(i), companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(companyVo.getDevelopState()));
						hotCompany.add(companyVo);
					}
				}else{
					for (RecruitCompanyInfoEntity recruitCompanyInfoEntity2 : recruitCompanyInfoEntity) {
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(recruitCompanyInfoEntity2, companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(recruitCompanyInfoEntity2.getDevelopState()));
						hotCompany.add(companyVo);
					}
				}
				
				modelMap.addAttribute("userName", userName);
				modelMap.addAttribute("hotJob", hotJob);
				modelMap.addAttribute("hotCompany", hotCompany);	
				return new ModelAndView("com/recruit/index/index");
				
			}else{
				modelMap.addAttribute("msg", 2);
				return new ModelAndView("com/recruit/user/login");
			}
		}	
		
	}
	
	@RequestMapping(params = "doRegister")
	@ResponseBody
	public int doRegister(HttpServletRequest request,ModelMap modelMap,String userName,String password) {
		RecruitUserEntity checkuser = systemService.findUniqueByProperty(RecruitUserEntity.class, "username", userName);
		if(StringUtil.isEmpty(checkuser)){
			RecruitUserEntity user = new RecruitUserEntity();
			user.setUsername(userName);
			user.setPassword(password);
			systemService.save(user);
			return 1;
		}else{
			return 0;
		}
	}
	
	
	@RequestMapping(params = "outLogin")
	public ModelAndView outLogin(HttpSession session,HttpServletRequest request,ModelMap modelMap) {
		
				if(session.getAttribute("username") != null){
					session.removeAttribute("username");
				}
		
				
				//随机获取4条热门岗位
				String hql = "from RecruitCompanyJobEntity order by RAND()";
				List<RecruitCompanyJobEntity> recruitCompanyJobEntity = systemService.findByQueryString(hql);
				List<IndexVo> hotJob = new ArrayList<>();
				IndexVo indexVo;
				if(recruitCompanyJobEntity.size()>4){
					for (int i=0;i<4;i++ ) {
						RecruitCompanyJobEntity job = new RecruitCompanyJobEntity();
						BeanUtils.copyProperties(recruitCompanyJobEntity.get(i), job);
						job.setEducation(TypeUtil.getEduName(job.getEducation()));
						
						RecruitCompanyInfoEntity company = systemService.findUniqueByProperty(RecruitCompanyInfoEntity.class, "userId", job.getCompanyId());
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(company, companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(company.getDevelopState()));
						indexVo = new IndexVo();
						indexVo.setJob(job);
						indexVo.setCompany(companyVo);
						hotJob.add(indexVo);
					}
				}else{
					for (RecruitCompanyJobEntity recruitCompanyJobEntity2 : recruitCompanyJobEntity) {
						RecruitCompanyJobEntity job = new RecruitCompanyJobEntity();
						BeanUtils.copyProperties(recruitCompanyJobEntity2, job);
						job.setEducation(TypeUtil.getEduName(job.getEducation()));
						RecruitCompanyInfoEntity company = systemService.findUniqueByProperty(RecruitCompanyInfoEntity.class, "userId", job.getCompanyId());
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(company, companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(company.getDevelopState()));
						indexVo = new IndexVo();
						indexVo.setJob(job);
						indexVo.setCompany(companyVo);
						hotJob.add(indexVo);
					}
				}
				
				//随机获取4个热门公司
				String hql2 = "from RecruitCompanyInfoEntity order by RAND()";
				List<RecruitCompanyInfoEntity> recruitCompanyInfoEntity = systemService.findByQueryString(hql2);
				List<RecruitCompanyInfoEntity> hotCompany = new ArrayList<>();
				if(recruitCompanyInfoEntity.size()>4){
					for (int i=0;i<4;i++ ) {
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(recruitCompanyInfoEntity.get(i), companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(companyVo.getDevelopState()));
						hotCompany.add(companyVo);
					}
				}else{
					for (RecruitCompanyInfoEntity recruitCompanyInfoEntity2 : recruitCompanyInfoEntity) {
						RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
						BeanUtils.copyProperties(recruitCompanyInfoEntity2, companyVo);
						companyVo.setDevelopState(TypeUtil.getDevelopState(recruitCompanyInfoEntity2.getDevelopState()));
						hotCompany.add(companyVo);
					}
				}
				modelMap.addAttribute("hotJob", hotJob);
				modelMap.addAttribute("hotCompany", hotCompany);	
				return new ModelAndView("com/recruit/index/index");

	}

}
